﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using SIMS.BL;

public partial class School_Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            if (Session[Global.Session_SchoolID] != null)
            {
                Int32 _schoolID = -1;
                if (Int32.TryParse(Session[Global.Session_SchoolID].ToString(), out _schoolID))
                {
                    ShowSchoolDetail(_schoolID);
                }
            }
        }
    }

    private void ShowSchoolDetail(int SchoolID)
    {
        School _item = new School();
        SchoolDB _db = new SchoolDB();
        _item = _db.GetItem(SchoolID);
        if (_item.Error.ErrorNo == 0)
        {
            hidSchoolID.Value = _item.SchoolID.ToString();
            txtSchoolName.Text = _item.SchoolName;
            txtSchoolShortName.Text = _item.ShortName;
            txtAddress.Text = _item.Address;
            txtCityName.Text = _item.City;
            txtStateName.Text = _item.State;
            txtPostCode.Text = _item.PostalCode;
            txtTelephone.Text = _item.Telephone;
            txtPrincipleName.Text = _item.PrincipleName;
            txtFax.Text = _item.FaxLine;
            txtEmail.Text = _item.EmailAddress;
            txtWebsiteUrl.Text = _item.WebsiteUrl;
            txtBaseGradingScale.Text = _item.BaseGradeScale.ToString("#.####");
        }
        else
        {
            lblMessage.Text = _item.Error.ErrorMessage;
        }
    }

    protected void btnSubmit_Click(object sender, EventArgs e)
    {
        Page.Validate("school");
        if (Page.IsValid)
        {
            School _item = new School();
            _item.SchoolID = Convert.ToInt32(hidSchoolID.Value);
            _item.SchoolName = txtSchoolName.Text;
            _item.ShortName = txtSchoolShortName.Text;
            _item.Address = txtAddress.Text;
            _item.City = txtCityName.Text;
            _item.State = txtStateName.Text;
            _item.PostalCode = txtPostCode.Text;
            _item.Telephone = txtTelephone.Text;
            _item.PrincipleName = txtPrincipleName.Text;
            _item.FaxLine = txtFax.Text;
            _item.EmailAddress = txtEmail.Text;
            _item.WebsiteUrl = txtWebsiteUrl.Text;
            _item.BaseGradeScale = Convert.ToDecimal(txtBaseGradingScale.Text);
            _item.SchoolLogoUrl = string.Empty;
            _item.IsActive = true;
            _item.CreatedBy = -1;
            _item.ModifiedBy = -1;

            SchoolDB _db = new SchoolDB();
            _item = _db.Save(_item);
            if (_item.Error.ErrorNo == 0)
            {
                hidSchoolID.Value = _item.SchoolID.ToString();
            }
            lblMessage.Text = _item.Error.ErrorMessage;
        }
    }
}